package com.bkq.cb;

import java.util.Calendar;

import com.bkq.cb.BLO.LoanBLO;
import com.bkq.cb.DTO.LoanDTO;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

public class BorrowScreen extends Activity{
	
	Button backBtn, saveBtn,cancelBtn;
	EditText namePersonTxt, dayFromTxt,dayToTxt, moneyTxt;
	LoanDTO loanObject = new LoanDTO();
	LoanBLO loanBlo = new LoanBLO(this);
	String idUser;
	public void onCreate(Bundle savedInstanceState) {
		
		
		 super.onCreate(savedInstanceState);
	     setContentView(R.layout.borrowscreen);
	     
	     backBtn = (Button)findViewById(R.id.backIma_borrowScreen);
	     saveBtn = (Button)findViewById(R.id.saveBnt_borrowScreen);
	     cancelBtn = (Button)findViewById(R.id.cancelBnt_borrowScreen);
	     namePersonTxt = (EditText) findViewById(R.id.lenderTxt_borrowScreen);
	     dayFromTxt = (EditText)findViewById(R.id.borrowingdayTxt_borrowScreen);
	     dayToTxt = (EditText)findViewById(R.id.returndayTxt_borrowScreen);
	     moneyTxt = (EditText)findViewById(R.id.moneyTxt_borrowScreen);
	     dayToTxt.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					// TODO Auto-generated method stub
					int y,m,d;
					Calendar c = Calendar.getInstance();        
					y = c.get(Calendar.YEAR);        
					m= c.get(Calendar.MONTH);        
					d = c.get(Calendar.DAY_OF_MONTH); 
					DatePickerDialog dp = new DatePickerDialog(BorrowScreen.this,new DatePickerDialog.OnDateSetListener() {
						@Override
						public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
							String erg = null;
							erg = String.valueOf(dayOfMonth);                      
							erg += "/" + String.valueOf(monthOfYear+1);                    
							erg += "/" + year;  
							dayToTxt.setText(erg);                   
						}	            	   
					}, y, m, d);
					dp.setTitle("Choose Date");
					dp.show();
				}
		});
	    dayFromTxt.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// TODO Auto-generated method stub
				int y,m,d;
				Calendar c = Calendar.getInstance();        
			    y = c.get(Calendar.YEAR);        
			    m= c.get(Calendar.MONTH);        
			    d = c.get(Calendar.DAY_OF_MONTH); 
			    DatePickerDialog dp = new DatePickerDialog(BorrowScreen.this,new DatePickerDialog.OnDateSetListener() {
			       	   @Override
			       	   public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
			       		   String erg = null;
			       		   erg = String.valueOf(dayOfMonth);                      
			       		   erg += "/" + String.valueOf(monthOfYear+1);                    
			       		   erg += "/" + year;
			       		   dayFromTxt.setText(erg);
			       	   }  
			    }, y, m, d);
			    dp.setTitle("Choose Date");
			    dp.show();
			}
		});
	    Calendar today = Calendar.getInstance();
	    Bundle extras = getIntent().getExtras();
		if (extras != null) 
			idUser = extras.getString("idUser").toString(); 
		    backBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					finish();
					
				}
			});
		    saveBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					loanObject.IdUser = Integer.parseInt(idUser);
					loanObject.namePerson = namePersonTxt.getText().toString();
					loanObject.dayFrom = dayFromTxt.getText().toString();
					loanObject.dayTo = dayToTxt.getText().toString();
					loanObject.money = moneyTxt.getText().toString();
					loanObject.type = "Borrow";
					if(loanBlo.insert(loanObject))
					{
						namePersonTxt.setText("");
						dayFromTxt.setText("");
						dayToTxt.setText("");
						moneyTxt.setText("");
						Toast.makeText(getBaseContext(), "insert success", Toast.LENGTH_SHORT).show();
					}
					else 
						Toast.makeText(getBaseContext(), "insert error", Toast.LENGTH_SHORT).show();
				}
			});
	        cancelBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				finish();
			}
	        });
	}
}
